package io.github.javpower.imagerex.controller;

import io.github.javpower.imagerex.model.Image;
import io.github.javpower.imagerex.req.ImageReq;
import io.github.javpower.imagerex.res.PageResult;
import io.github.javpower.imagerex.service.VectorExService;
import io.github.javpower.vectorexbootstater.core.VectoRexResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/image")
@Tag(name = "图片管理", description = "用于管理图片及其特征向量的接口")
public class ImageController {

    private final VectorExService vectorExService;

    @Autowired
    public ImageController(VectorExService vectorExService) {
        this.vectorExService = vectorExService;
    }

    @PostMapping("/delete")
    @Operation(summary = "删除图片", description = "根据图片名称删除图片及其特征向量")
    public String deleteImageByName(@RequestBody ImageReq req) {
        return vectorExService.deleteImageByName(req.getName());
    }

    @PostMapping("/upload/single")
    @Operation(summary = "上传单张图片", description = "上传单张图片并提取特征向量")
    public String uploadSingleImage(@RequestParam("file") MultipartFile file) {
        return vectorExService.uploadSingleImage(file);
    }

    @PostMapping("/upload/zip")
    @Operation(summary = "上传zip文件", description = "上传zip文件，自动解压并处理其中的图片")
    public String uploadZipFile(@RequestParam("file") MultipartFile file) {
        return vectorExService.uploadZipFile(file);
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询图片", description = "根据请求参数分页查询图片列表")
    public PageResult<Image> page(@RequestBody ImageReq req) {
        return vectorExService.page(req);
    }

    @PostMapping("/search")
    @Operation(summary = "搜索相似图片", description = "根据上传的图片搜索相似图片，返回前topK个结果")
    public List<VectoRexResult<Image>> search(@RequestParam("file") MultipartFile file, @RequestParam("topK") Integer topK) {
        return vectorExService.search(file, topK);
    }
}